<template>
    <div class="container_box">
        <el-scrollbar class="main_box">
            <div class="content">
                <el-form ref="ruleFormRef" :rules="rules" :model="formData" label-width="120px">
                    <el-form-item label="标签规则名称" prop="title">
                        <el-input
                            v-model.trim="formData.title"
                            placeholder="请输入标签规则名称"
                            maxlength="15"
                            show-word-limit
                            style="width: 580px">
                        </el-input>
                    </el-form-item>
                    <el-form-item label="描述">
                        <el-input
                            v-model="formData.desc"
                            placeholder="请输入描述"
                            type="textarea"
                            :rows="4"
                            maxlength="150"
                            show-word-limit
                            style="width: 580px">
                        </el-input>
                    </el-form-item>
                    <el-form-item label="有效期" prop="validity_type">
                        <el-radio-group v-model="formData.validity_type" @change="changeValidity">
                            <el-radio :label="1">永久</el-radio>
                            <el-radio :label="2">固定期限</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item v-show="isShowDate">
                        <div style="width: 380px">
                            <el-date-picker
                                v-model="dataTimes"
                                type="datetimerange"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期"
                                format="YYYY-MM-DD HH:mm:ss"
                                value-format="YYYY-MM-DD HH:mm:ss"
                                :disabled-date="disabledDate"
                                @change="changeDate">
                            </el-date-picker>
                        </div>
                    </el-form-item>
                    <el-form-item class="tag-item" label="打标规则" prop="mark_type">
                        <div>
                            <el-tabs v-model="formData.mark_type" class="tagRules">
                                <el-tab-pane label="客户属性" :name="1">
                                    <customer ref="customerRef" :cust_property="formData.cust_property"></customer>
                                </el-tab-pane>
                                <el-tab-pane label="沟通属性" :name="2">
                                    <communicate ref="communicateRef" :connect_property="formData.connect_property">
                                    </communicate>
                                </el-tab-pane>
                                <el-tab-pane label="销售属性" :name="3">
                                    <sale ref="saleRef" :business="formData.business"> </sale>
                                </el-tab-pane>
                                <el-tab-pane label="客户行为" :name="4">
                                    <customerBhavior
                                        ref="customerBhaviorRef"
                                        :cust_property_behaviour="formData.cust_property_behaviour">
                                    </customerBhavior>
                                </el-tab-pane>
                                <el-tab-pane label="电商属性" :name="5">
                                    <onlineRetailers
                                        ref="onlineRetailersRef"
                                        :retail_property="formData.retail_property">
                                    </onlineRetailers>
                                </el-tab-pane>
                            </el-tabs>
                            <div class="tag-info">
                                <span class="tag-title">预计打标客户数：</span>
                                <span v-show="!isShowNum" class="cust_total" @click="seeNum">查看</span>
                                <span v-show="isShowNum" class="cust_total">{{ cust_total }}人</span>
                                <span v-show="isShowNum" class="cust_total" @click="seeNum">刷新</span>
                            </div>
                        </div>
                    </el-form-item>
                    <el-form-item label="自动打标" prop="cust_tag_list">
                        <div>
                            <el-button @click="opentag">选择标签</el-button>
                            <div class="zw-el-tags">
                                <el-tag v-for="(item, index) in taglist" :key="index" closable @close="closetag(item)">
                                    {{ item.name }}
                                </el-tag>
                            </div>
                        </div>
                    </el-form-item>
                    <el-form-item label="执行周期" prop="execute_time">
                        <div class="radio-group">
                            每<el-input
                                v-model.number="formData.execute_time"
                                onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')"
                                style="width: 90px"
                                class="radio-item"
                                controls-position="right" />
                            <el-select
                                v-model="formData.execute_time_type"
                                class="radio-item"
                                style="width: 90px"
                                @change="changeType">
                                <el-option value="hour" label="小时">小时</el-option>
                                <el-option value="day" label="日">日</el-option>
                                <el-option value="month" label="月">月</el-option>
                            </el-select>
                            <el-time-select
                                v-show="isTimeType"
                                v-model="formData.execute_time_hour"
                                start="00:00"
                                step="00:15"
                                end="23:45"
                                style="width: 180px"
                                class="radio-item"
                                placeholder="选择时间" />
                            {{ timeType == 'month' ? '1号执行' : '执行' }}
                        </div>
                    </el-form-item>
                </el-form>
            </div>
            <BatchLabel ref="BatchLabelRef" @save="getTagsFun" />
        </el-scrollbar>
        <div class="footer_box">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="save(ruleFormRef)">保存</el-button>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, toRefs, watch } from 'vue';
import customer from './components/customer.vue';
import communicate from './components/communicate.vue';
import sale from './components/sale.vue';
import customerBhavior from './components/customerBhavior.vue';
import onlineRetailers from './components/onlineRetailers.vue';
import BatchLabel from '@/components/BatchLabel.vue';
import { createRule, ruleScreen, ruleInfo, ruleUpdate } from '@/api/rules';
import router from '@/router';
import { ElMessage, TabsPaneContext } from 'element-plus';
import { useRoute } from 'vue-router';
import { IsNotEmpty } from '@/utils/utils';
const route = useRoute();
const ruleFormRef = ref(null);
const BatchLabelRef = ref(null);
const communicateRef = ref(null);
const customerRef = ref(null);
const saleRef = ref(null);
const customerBhaviorRef = ref(null);
const onlineRetailersRef = ref(null);
const isTimeType = ref(false);
const taglist = ref([]);
const cust_tag_id = ref();
const formData = reactive({
    title: '', //标签规则名称
    desc: '', //描述
    validity_type: 1, //1 长久有效 2
    start_time: '',
    end_time: '',
    mark_type: 1, //1 客户属性 2沟通属性 3销售属性 4电商属性
    cust_tag_list: [],
    execute_time: 1,
    execute_time_type: 'hour',
    execute_time_hour: '',
    cust_property: {}, //客户属性
    connect_property: {}, //沟通属性
    retail_property: {}, //电商属性
    business: {}, //销售属性
    cust_property_behaviour: {} //客户行为
});
const dataTimes = ref([]); //有效期
const cust_total = ref(0);
const isShowNum = ref(false);
const isShowDate = ref(false);
const timeType = ref();
// 创建规则校验
const rules = reactive({
    title: [
        {
            required: true,
            message: '请输入标签规则名称',
            trigger: 'blur'
        }
    ],
    validity_type: [
        {
            required: true,
            message: '请选择活动有效期',
            trigger: 'change'
        }
    ],
    mark_type: [
        {
            required: true,
            message: '请选择打标规则',
            trigger: 'change'
        }
    ],
    cust_tag_list: [
        {
            required: true,
            message: '请选择标签',
            trigger: 'change'
        }
    ],
    execute_time: [
        {
            required: true,
            message: '请输入执行周期',
            trigger: 'change'
        }
    ]
});
//禁用之前的时间
const disabledDate = (time: any) => {
    return time.getTime() < Date.now() - 8.64e7;
};
//切换活动有效期哦
const changeValidity = (val: any) => {
    val == 1 ? (isShowDate.value = false) : (isShowDate.value = true);
    val == 1 ? (dataTimes.value = []) : '';
};
//切换有效期
const changeDate = () => {};
//取消
const cancel = () => {
    router.go(-1);
};
//切换type类型
const changeType = (val: any) => {
    timeType.value = val;
    val == 'day' ? (isTimeType.value = true) : (isTimeType.value = false);
};
const formatUnlimited = (val: any) => {
    if (val.length > 0 && !val.includes(-1)) {
        return true;
    }
    return false;
};
const formatStyle = (val: any) => {
    const dataType = typeof val;
    if (dataType == 'string' && val != '') {
        return true;
    }
    if (val) {
        if (dataType == 'object' && val.length > 0) {
            return true;
        }
    }
    return false;
};
// 非空校验 客户属性
const validateCustomer = (): boolean => {
    return Object.keys(customerRef.value.formData).some(key => {
        let flag = false;
        switch (key) {
            case 'sex':
                flag = formatUnlimited(customerRef.value.formData[key]);
                break;
            case 'age':
                flag = formatUnlimited(customerRef.value.formData[key]);
                break;
            case 'chat_group_all':
                if (customerRef.value.formData[key] > 0) {
                    flag = true;
                }
                break;
            case 'cust_tag_list_all':
                if (customerRef.value.formData[key] > 0) {
                    flag = true;
                } else {
                    if (customerRef.value.addTimes == 0) {
                        flag = true;
                    }
                }
                break;
            case 'add_friend_day':
                if (customerRef.value.formData[key] > 0) {
                    flag = true;
                } else {
                    if (customerRef.value.custTimes == 0) {
                        flag = true;
                    }
                }
                break;
            case 'add_cust_day':
                if (customerRef.value.formData[key] > 0) {
                    flag = true;
                }
                break;
            default:
                flag = formatStyle(customerRef.value.formData[key]);
                break;
        }
        return flag;
    });
};
// 非空校验 沟通属性
const validateCommunication = (): boolean => {
    let flag = false;
    return Object.keys(communicateRef.value.formData).some(key => {
        switch (key) {
            case 'customer_phase':
                flag = formatUnlimited(communicateRef.value.formData[key]);
                break;
            case 'chat_reply':
                if (communicateRef.value.formData[key] == 1) {
                    flag = true;
                }
                break;
            case 'chat_reply_time':
                if (communicateRef.value.formData[key]) {
                    flag = false;
                }
                break;
            case 'chat_reply_time_type':
                if (communicateRef.value.formData[key]) {
                    flag = false;
                }
                break;
            case 'follow_up_times_min':
                if (communicateRef.value.formData[key] > 0) {
                    flag = true;
                }
                break;
            case 'follow_up_times_max':
                if (communicateRef.value.formData[key] > 0) {
                    flag = true;
                }
                break;
            case 'last_follow_up_day':
                if (communicateRef.value.formData[key] > 0) {
                    flag = true;
                } else {
                    if (communicateRef.value.fllowTime == 0) {
                        flag = true;
                    }
                }
                break;
            case 'last_chat_day':
                if (communicateRef.value.formData[key] > 0) {
                    flag = true;
                } else {
                    if (communicateRef.value.chatTime == 0) {
                        flag = true;
                    }
                }
                break;
            default:
                flag = formatStyle(communicateRef.value.formData[key]);
                break;
        }
        return flag;
    });
};
// 非空校验 销售属性
const validateSales = (): boolean => {
    let flag = false;
    return Object.keys(saleRef.value.formData).some(key => {
        switch (key) {
            case 'specify_process':
                flag = formatUnlimited(saleRef.value.formData[key]);
                break;
            case 'business_param':
                return Object.keys(saleRef.value.formData[key]).some(key1 => {
                    flag = formatStyle(saleRef.value.formData[key][key1]);
                    return flag;
                });
            case 'win_order_day':
                if (saleRef.value.formData[key] > 0) {
                    flag = true;
                } else {
                    if (saleRef.value.fllowTime == 0) {
                        flag = true;
                    }
                }
                break;
            default:
                flag = formatStyle(saleRef.value.formData[key]);
                break;
        }
        return flag;
    });
};
// 非空校验 客户行为
const validateBehavior = (): boolean => {
    let flag = false;
    return Object.keys(customerBhaviorRef.value.formData).some(key => {
        switch (key) {
            case 'comment_type':
                if (customerBhaviorRef.value.formData[key] == 2) {
                    flag = true;
                }
                break;
            case 'shop_source':
                flag = formatUnlimited(customerBhaviorRef.value.formData[key]);
                break;
            case 'visit_source':
                flag = formatUnlimited(customerBhaviorRef.value.formData[key]);
                break;
            case 'behaviour':
                flag = formatUnlimited(customerBhaviorRef.value.formData[key]);
                break;
            case 'behaviour_day':
                if (customerBhaviorRef.value.formData[key] > 0) {
                    flag = true;
                } else {
                    if (customerBhaviorRef.value.fllowTime == 0) {
                        flag = true;
                    }
                }
                break;
            default:
                flag = formatStyle(customerBhaviorRef.value.formData[key]);
                break;
        }
        return flag;
    });
};
// 非空校验 电商属性
const validateEcommerce = (): boolean => {
    let flag = false;
    return Object.keys(onlineRetailersRef.value.formData).some(key => {
        switch (key) {
            case 'shop_type':
                flag = formatUnlimited(onlineRetailersRef.value.formData[key]);
                break;
            case 'shop_id':
                flag = formatUnlimited(onlineRetailersRef.value.formData[key]);
                break;
            case 'last_consume_day':
                if (onlineRetailersRef.value.formData[key] > 0) {
                    flag = true;
                } else {
                    if (onlineRetailersRef.value.fllowTime == 0) {
                        flag = true;
                    }
                }
                break;
            default:
                flag = formatStyle(onlineRetailersRef.value.formData[key]);
                break;
        }
        return flag;
    });
};

const validateLableRules = (): boolean => {
    return (
        validateCustomer() || validateCommunication() || validateSales() || validateBehavior() || validateEcommerce()
    );
};

//创建规则
const save = (formEl: any) => {
    if (!formEl) return;
    formEl.validate((valid: any) => {
        if (valid) {
            //头部
            if (formData.validity_type == 2) {
                if (!dataTimes.value || dataTimes.value.length == 0) {
                    ElMessage.error('请选择固定期限的开始时间和结束时间');
                    return;
                } else if (dataTimes.value[0] == dataTimes.value[1]) {
                    ElMessage.error('固定期限的开始时间和结束时间不可相同');
                    return;
                } else {
                    formData.start_time = dataTimes.value[0];
                    formData.end_time = dataTimes.value[1];
                }
            }

            // 打标规则 非空校验
            const flag = validateLableRules();
            if (!flag) {
                // if(true){
                ElMessage.error('至少选择一个打标规则！');
                return;
            }

            //客户属性
            formData.cust_property = JSON.parse(JSON.stringify(customerRef.value.formData));
            if (customerRef.value.addTimes == 0 && !IsNotEmpty(formData.cust_property.add_friend_time)) {
                ElMessage.error('请选择添加时间');
                return;
            }
            if (customerRef.value.custTimes == 0 && !IsNotEmpty(formData.cust_property.add_cust_time)) {
                ElMessage.error('请选择创建时间');
                return;
            }
            formData.cust_property.cust_source.length > 0
                ? (formData.cust_property.cust_source_all = 2)
                : (formData.cust_property.cust_source_all = 0);
            if (formData.cust_property.cust_source.includes(0)) {
                formData.cust_property.cust_source = [];
                formData.cust_property.cust_source_all = 1;
            }
            if (!formData.cust_property.chat_group_all) {
                formData.cust_property.chat_group_all = 0;
            } else {
                if (formData.cust_property.chat_group.length == 0) {
                    ElMessage.error('请选择群聊');
                    return;
                }
            }
            if (!formData.cust_property.cust_tag_list_all) {
                formData.cust_property.cust_tag_list_all = 0;
            } else {
                if (formData.cust_property.cust_tag_list.length == 0) {
                    ElMessage.error('请选择标签');
                    return;
                }
            }
            getAttribute();
        }
    });
};
//沟通属性、销售属性、客户行为、电商属性
const getAttribute = () => {
    //沟通属性
    formData.connect_property = JSON.parse(JSON.stringify(communicateRef.value.formData));
    if (formData.connect_property.follow_up_times_min > 0 && formData.connect_property.follow_up_times_max > 0) {
        if (formData.connect_property.follow_up_times_min > formData.connect_property.follow_up_times_max) {
            ElMessage.error('跟进次数最小值不能大于最大值');
            return;
        }
    }
    if (formData.connect_property.chat_reply == 1) {
        if (!formData.connect_property.chat_reply_time || !formData.connect_property.chat_reply_time_type) {
            ElMessage.error('请输入聊天回复');
            return;
        }
    }
    if (communicateRef.value.fllowTime == 0 && !IsNotEmpty(formData.connect_property.last_follow_up_time)) {
        ElMessage.error('请选择最后跟进时间');
        return;
    }
    if (communicateRef.value.chatTime == 0 && !IsNotEmpty(formData.connect_property.last_chat_time)) {
        ElMessage.error('请选择上次聊天时间');
        return;
    }
    if (isTimeType.value && !formData.execute_time_hour) {
        ElMessage.error('请选择执行周期时间');
        return;
    }
    //销售属性
    formData.business = JSON.parse(JSON.stringify(saleRef.value.formData));
    if (saleRef.value.fllowTime == 0 && !IsNotEmpty(formData.business.win_order_time)) {
        ElMessage.error('请选择赢单日期');
        return;
    }
    if (
        formData.business.business_param.lost_num_between[0] &&
        (formData.business.business_param.lost_num_between[1] ||
            formData.business.business_param.lost_num_between[1] === 0)
    ) {
        if (
            formData.business.business_param.lost_num_between[0] > formData.business.business_param.lost_num_between[1]
        ) {
            ElMessage.error('输单数量最小值不可大于最大值');
            return;
        }
    }
    if (
        formData.business.business_param.win_num_between[0] &&
        (formData.business.business_param.win_num_between[1] ||
            formData.business.business_param.win_num_between[1] === 0)
    ) {
        if (formData.business.business_param.win_num_between[0] > formData.business.business_param.win_num_between[1]) {
            ElMessage.error('赢单数量最小值不可大于最大值');
            return;
        }
    }
    if (
        formData.business.business_param.business_amount[0] &&
        (formData.business.business_param.business_amount[1] ||
            formData.business.business_param.business_amount[1] === 0)
    ) {
        if (formData.business.business_param.business_amount[0] > formData.business.business_param.business_amount[1]) {
            ElMessage.error('商机金额下限不可大于上限');
            return;
        }
    }
    if (
        formData.business.business_param.win_amount[0] &&
        (formData.business.business_param.win_amount[1] || formData.business.business_param.win_amount[1] === 0)
    ) {
        if (formData.business.business_param.win_amount[0] > formData.business.business_param.win_amount[1]) {
            ElMessage.error('赢单金额下限不可大于上限');
            return;
        }
    }
    if (
        formData.business.business_param.win_order_amount[0] &&
        (formData.business.business_param.win_order_amount[1] ||
            formData.business.business_param.win_order_amount[1] === 0)
    ) {
        if (
            formData.business.business_param.win_order_amount[0] > formData.business.business_param.win_order_amount[1]
        ) {
            ElMessage.error('赢单订单金额下限不可大于上限');
            return;
        }
    }
    if (formData.business.specify_process) {
        formData.business.specify_process.forEach((item: any) => {
            if (item == -1) {
                formData.business.specify_process = formData.business.specify_process.filter((item: any) => {
                    return item != -1;
                });
            }
        });
    }
    //电商属性
    formData.retail_property = JSON.parse(JSON.stringify(onlineRetailersRef.value.formData));
    if (onlineRetailersRef.value.fllowTime == 0 && !IsNotEmpty(formData.retail_property.last_consume_time)) {
        ElMessage.error('请选择最后消费时间');
        return;
    }
    if (
        formData.retail_property.single_consume_amount[0] &&
        (formData.retail_property.single_consume_amount[1] || formData.retail_property.single_consume_amount[1]) === 0
    ) {
        if (formData.retail_property.single_consume_amount[0] > formData.retail_property.single_consume_amount[1]) {
            ElMessage.error('单次消费金额下限不可大于上限');
            return;
        }
    }
    if (
        formData.retail_property.cumulative_consume_amount[0] &&
        (formData.retail_property.cumulative_consume_amount[1] ||
            formData.retail_property.cumulative_consume_amount[1] === 0)
    ) {
        if (
            formData.retail_property.cumulative_consume_amount[0] >
            formData.retail_property.cumulative_consume_amount[1]
        ) {
            ElMessage.error('累计消费金额下限不可大于上限');
            return;
        }
    }
    if (
        formData.retail_property.cumulative_consume_frequency[0] > 0 &&
        formData.retail_property.cumulative_consume_frequency[1] > 0
    ) {
        if (
            formData.retail_property.cumulative_consume_frequency[0] >
            formData.retail_property.cumulative_consume_frequency[1]
        ) {
            ElMessage.error('累计消费次数最小值不可大于最大值');
            return;
        }
    }
    if (formData.retail_property.consume_frequency[0] > 0 && formData.retail_property.consume_frequency[1] > 0) {
        if (formData.retail_property.consume_frequency[0] > formData.retail_property.consume_frequency[1]) {
            ElMessage.error('消费频率最小值不可大于最大值');
            return;
        }
    }
    if (formData.retail_property.single_piece[0] > 0 && formData.retail_property.single_piece[1] > 0) {
        if (formData.retail_property.single_piece[0] > formData.retail_property.single_piece[1]) {
            ElMessage.error('客单件最小值不可大于最大值');
            return;
        }
    }
    if (
        formData.retail_property.single_price[0] &&
        (formData.retail_property.single_price[1] || formData.retail_property.single_price[1] === 0)
    ) {
        if (formData.retail_property.single_price[0] > formData.retail_property.single_price[1]) {
            ElMessage.error('客单价金额下限不可大于上限');
            return;
        }
    }
    if (formData.retail_property.shop_type) {
        formData.retail_property.shop_type.forEach((item: any) => {
            if (item == -1) {
                formData.retail_property.shop_type = formData.retail_property.shop_type.filter((item: any) => {
                    return item != -1;
                });
            }
        });
    }
    if (formData.retail_property.shop_id) {
        formData.retail_property.shop_id.forEach((item: any) => {
            if (item == -1) {
                formData.retail_property.shop_id = formData.retail_property.shop_id.filter((item: any) => {
                    return item != -1;
                });
            }
        });
    }

    //客户行为
    formData.cust_property_behaviour = JSON.parse(JSON.stringify(customerBhaviorRef.value.formData));
    if (customerBhaviorRef.value.fllowTime == 0 && !IsNotEmpty(formData.cust_property_behaviour.behaviour_time)) {
        ElMessage.error('请选择行为时间');
        return;
    }
    if (
        formData.cust_property_behaviour.visit_duration[0] &&
        (formData.cust_property_behaviour.visit_duration[1] || formData.cust_property_behaviour.visit_duration[1] === 0)
    ) {
        if (formData.cust_property_behaviour.visit_duration[0] > formData.cust_property_behaviour.visit_duration[1]) {
            ElMessage.error('浏览时长最小值不可大于最大值');
            return;
        }
    }
    if (
        formData.cust_property_behaviour.comment_type == 2 &&
        formData.cust_property_behaviour.comment_keywords.length == 0
    ) {
        ElMessage.error('请输入评论关键词');
        return;
    }
    formData.cust_property_behaviour.shop_source.sort((a, b) => {
        return a - b;
    });
    onlineRetailersRef.value.retailRef.validate((valid: any) => {
        if (valid) {
            if (!cust_tag_id.value) {
                createRule(formData).then((res: any) => {
                    ElMessage.success('创建成功');
                    cancel();
                });
            } else {
                const params = {
                    cust_rule_tag_id: cust_tag_id.value,
                    ...formData
                };
                ruleUpdate(params).then((res: any) => {
                    ElMessage.success('编辑成功');
                    cancel();
                });
            }
        }
    });
};
//查看消息预计送达人数
const seeNum = () => {
    const formData = JSON.parse(JSON.stringify(customerRef.value.formData));
    if (customerRef.value.addTimes == 0 && !IsNotEmpty(formData.add_friend_time)) {
        ElMessage.error('请选择添加时间');
        return;
    }
    if (customerRef.value.custTimes == 0 && !IsNotEmpty(formData.add_cust_time)) {
        ElMessage.error('请选择创建时间');
        return;
    }
    formData.cust_source.length > 0 ? (formData.cust_source_all = 2) : (formData.cust_source_all = 0);
    if (formData.cust_source.includes(0)) {
        formData.cust_source = [];
        formData.cust_source_all = 1;
    }
    if (!formData.chat_group_all) {
        formData.chat_group_all = 0;
    } else {
        if (formData.chat_group.length == 0) {
            ElMessage.error('请选择群聊');
            return;
        }
    }
    if (!formData.cust_tag_list_all) {
        formData.cust_tag_list_all = 0;
    } else {
        if (formData.cust_tag_list.length == 0) {
            ElMessage.error('请选择标签');
            return;
        }
    }
    //沟通属性
    formData.connect_property = JSON.parse(JSON.stringify(communicateRef.value.formData));
    if (formData.connect_property.follow_up_times_min > 0 && formData.connect_property.follow_up_times_max > 0) {
        if (formData.connect_property.follow_up_times_min > formData.connect_property.follow_up_times_max) {
            ElMessage.error('跟进次数最小值不能大于最大值');
            return;
        }
    }
    if (formData.connect_property.chat_reply == 1) {
        if (!formData.connect_property.chat_reply_time || !formData.connect_property.chat_reply_time_type) {
            ElMessage.error('请输入聊天回复');
            return;
        }
    }
    if (communicateRef.value.fllowTime == 0 && !IsNotEmpty(formData.connect_property.last_follow_up_time)) {
        ElMessage.error('请选择最后跟进时间');
        return;
    }
    if (communicateRef.value.chatTime == 0 && !IsNotEmpty(formData.connect_property.last_chat_time)) {
        ElMessage.error('请选择上次聊天时间');
        return;
    }
    //销售属性
    formData.business = JSON.parse(JSON.stringify(saleRef.value.formData));
    if (saleRef.value.fllowTime == 0 && !IsNotEmpty(formData.business.win_order_time)) {
        ElMessage.error('请选择赢单日期');
        return;
    }
    if (
        formData.business.business_param.lost_num_between[0] &&
        (formData.business.business_param.lost_num_between[1] ||
            formData.business.business_param.lost_num_between[1] === 0)
    ) {
        if (
            formData.business.business_param.lost_num_between[0] > formData.business.business_param.lost_num_between[1]
        ) {
            ElMessage.error('输单数量最小值不可大于最大值');
            return;
        }
    }
    if (
        formData.business.business_param.win_num_between[0] &&
        (formData.business.business_param.win_num_between[1] ||
            formData.business.business_param.win_num_between[1] === 0)
    ) {
        if (formData.business.business_param.win_num_between[0] > formData.business.business_param.win_num_between[1]) {
            ElMessage.error('赢单数量最小值不可大于最大值');
            return;
        }
    }
    if (
        formData.business.business_param.business_amount[0] &&
        (formData.business.business_param.business_amount[1] ||
            formData.business.business_param.business_amount[1] === 0)
    ) {
        if (formData.business.business_param.business_amount[0] > formData.business.business_param.business_amount[1]) {
            ElMessage.error('商机金额下限不可大于上限');
            return;
        }
    }
    if (
        formData.business.business_param.win_amount[0] &&
        (formData.business.business_param.win_amount[1] || formData.business.business_param.win_amount[1] === 0)
    ) {
        if (formData.business.business_param.win_amount[0] > formData.business.business_param.win_amount[1]) {
            ElMessage.error('赢单金额下限不可大于上限');
            return;
        }
    }
    if (
        formData.business.business_param.win_order_amount[0] &&
        (formData.business.business_param.win_order_amount[1] ||
            formData.business.business_param.win_order_amount[1] === 0)
    ) {
        if (
            formData.business.business_param.win_order_amount[0] > formData.business.business_param.win_order_amount[1]
        ) {
            ElMessage.error('赢单订单金额下限不可大于上限');
            return;
        }
    }
    if (formData.business.specify_process) {
        formData.business.specify_process.forEach((item: any) => {
            if (item == -1) {
                formData.business.specify_process = formData.business.specify_process.filter((item: any) => {
                    return item != -1;
                });
            }
        });
    }
    //电商属性
    formData.retail_property = JSON.parse(JSON.stringify(onlineRetailersRef.value.formData));
    if (onlineRetailersRef.value.fllowTime == 0 && !IsNotEmpty(formData.retail_property.last_consume_time)) {
        ElMessage.error('请选择最后消费时间');
        return;
    }
    if (
        formData.retail_property.single_consume_amount[0] &&
        (formData.retail_property.single_consume_amount[1] || formData.retail_property.single_consume_amount[1]) === 0
    ) {
        if (formData.retail_property.single_consume_amount[0] > formData.retail_property.single_consume_amount[1]) {
            ElMessage.error('单次消费金额下限不可大于上限');
            return;
        }
    }
    if (
        formData.retail_property.cumulative_consume_amount[0] &&
        (formData.retail_property.cumulative_consume_amount[1] ||
            formData.retail_property.cumulative_consume_amount[1] === 0)
    ) {
        if (
            formData.retail_property.cumulative_consume_amount[0] >
            formData.retail_property.cumulative_consume_amount[1]
        ) {
            ElMessage.error('累计消费金额下限不可大于上限');
            return;
        }
    }
    if (
        formData.retail_property.cumulative_consume_frequency[0] > 0 &&
        formData.retail_property.cumulative_consume_frequency[1] > 0
    ) {
        if (
            formData.retail_property.cumulative_consume_frequency[0] >
            formData.retail_property.cumulative_consume_frequency[1]
        ) {
            ElMessage.error('累计消费次数最小值不可大于最大值');
            return;
        }
    }
    if (formData.retail_property.consume_frequency[0] > 0 && formData.retail_property.consume_frequency[1] > 0) {
        if (formData.retail_property.consume_frequency[0] > formData.retail_property.consume_frequency[1]) {
            ElMessage.error('消费频率最小值不可大于最大值');
            return;
        }
    }
    if (formData.retail_property.single_piece[0] > 0 && formData.retail_property.single_piece[1] > 0) {
        if (formData.retail_property.single_piece[0] > formData.retail_property.single_piece[1]) {
            ElMessage.error('客单件最小值不可大于最大值');
            return;
        }
    }
    if (
        formData.retail_property.single_price[0] &&
        (formData.retail_property.single_price[1] || formData.retail_property.single_price[1] === 0)
    ) {
        if (formData.retail_property.single_price[0] > formData.retail_property.single_price[1]) {
            ElMessage.error('客单价金额下限不可大于上限');
            return;
        }
    }
    if (formData.retail_property.shop_type) {
        formData.retail_property.shop_type.forEach((item: any) => {
            if (item == -1) {
                formData.retail_property.shop_type = formData.retail_property.shop_type.filter((item: any) => {
                    return item != -1;
                });
            }
        });
    }
    if (formData.retail_property.shop_id) {
        formData.retail_property.shop_id.forEach((item: any) => {
            if (item == -1) {
                formData.retail_property.shop_id = formData.retail_property.shop_id.filter((item: any) => {
                    return item != -1;
                });
            }
        });
    }

    //客户行为
    formData.cust_property_behaviour = JSON.parse(JSON.stringify(customerBhaviorRef.value.formData));
    if (customerBhaviorRef.value.fllowTime == 0 && !IsNotEmpty(formData.cust_property_behaviour.behaviour_time)) {
        ElMessage.error('请选择行为时间');
        return;
    }
    if (
        formData.cust_property_behaviour.visit_duration[0] &&
        (formData.cust_property_behaviour.visit_duration[1] || formData.cust_property_behaviour.visit_duration[1] === 0)
    ) {
        if (formData.cust_property_behaviour.visit_duration[0] > formData.cust_property_behaviour.visit_duration[1]) {
            ElMessage.error('浏览时长最小值不可大于最大值');
            return;
        }
    }
    if (
        formData.cust_property_behaviour.comment_type == 2 &&
        formData.cust_property_behaviour.comment_keywords.length == 0
    ) {
        ElMessage.error('请输入评论关键词');
        return;
    }
    onlineRetailersRef.value.retailRef.validate((valid: any) => {
        if (valid) {
            ruleScreen(formData).then((res: any) => {
                cust_total.value = res.cust_total;
                isShowNum.value = true;
            });
        }
    });
};

const opentag = () => {
    BatchLabelRef.value.hasConditiion = false;
    BatchLabelRef.value.dialogData.labelChooseType = 2;
    BatchLabelRef.value.chooseDataArr = [];
    if (taglist.value.length > 0) {
        BatchLabelRef.value.chooseDataArr = [...taglist.value];
    }
    BatchLabelRef.value.dialogVisible = true;
};
const getTagsFun = (data: any) => {
    formData.cust_tag_list = [];
    taglist.value = [...data.data];
    taglist.value.forEach((item: any) => {
        formData.cust_tag_list.push({ id: item.id, name: item.name });
    });
};
const closetag = (res: any) => {
    formData.cust_tag_list = [];
    taglist.value = taglist.value.filter((item: any) => {
        return item.id != res.id;
    });
    taglist.value.forEach((item: any) => {
        formData.cust_tag_list.push({ id: item.id, name: item.name });
    });
};
//获取标签规则详情
const getRulesDetail = () => {
    const params = {
        cust_rule_tag_id: cust_tag_id.value
    };
    ruleInfo(params).then((res: any) => {
        formData.title = res.title;
        formData.desc = res.desc;
        formData.cust_property = res.cust_property;
        formData.connect_property = res.connect_property_rule;
        formData.business = res.business;
        formData.retail_property = res.retail_property;
        formData.cust_property_behaviour = res.cust_property_behaviour;
        formData.validity_type = res.validity_type;
        formData.start_time = res.start_time;
        formData.end_time = res.end_time;
        dataTimes.value = [res.start_time, res.end_time];
        formData.cust_tag_list = res.cust_tag_list;
        formData.execute_time = res.execute_time;
        formData.execute_time_type = res.execute_time_type;
        formData.execute_time_hour = res.execute_time_hour;
        taglist.value = res.cust_tag_list;
        formData.validity_type == 2 ? (isShowDate.value = true) : (isShowDate.value = false);
        formData.execute_time_type == 'day' ? (isTimeType.value = true) : (isTimeType.value = false);
    });
};
watch(
    () => formData.cust_tag_list,
    () => {
        ruleFormRef.value.validateField('cust_tag_list');
    }
);
onMounted(() => {
    cust_tag_id.value = route.query.custId;
    if (cust_tag_id.value) {
        getRulesDetail();
    }
});
</script>
<style scoped lang="scss">
.content {
    padding: 24px;
}

.tag-item {
    .tagRules {
        border: 1px solid #dcdfe6;
        padding: 20px;
    }

    .tag-info {
        font-size: 12px;
        top: 2px;
        right: 20px;
        color: #ff6b00;

        .tag-title {
            color: #585858;
        }

        .cust_total {
            cursor: pointer;
            margin-right: 5px;
        }
    }
}

.radio-group {
    display: flex;

    .radio-item {
        margin: 0 5px;
    }
}

.handleBtn {
    text-align: center;
}
</style>
